package buzzword.gui;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.Rectangle;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import buzzword.Game;
import buzzword.GameEndedException;
import buzzword.GameNotStartetException;
import buzzword.ServerCommunicationException;

import json.JSONException;

public class ListAllGamesPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JScrollPane jScrollPane = null;
	private JButton buttonJoinGame = null;
	private JList jList = null;
	private JLabel labelTextPlayerName = null;
	private JTextField textFieldEnterName = null;
	private JButton buttonBack = null;
	private JLabel labelWaiting = null;
//	private List<Game> listOfGames; // @jve:decl-index=0:

	/**
	 * This is the default constructor
	 */
	public ListAllGamesPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		labelWaiting = new JLabel("Waiting", SwingConstants.CENTER);
		labelWaiting.setVisible(false);
		labelWaiting.setBounds(new Rectangle(214, 226, 161, 45));
		labelTextPlayerName = new JLabel("Playername", SwingConstants.CENTER);
		labelTextPlayerName.setBounds(new Rectangle(210, 120, 166, 31));
		this.setSize(400, 400);
		this.setLayout(null);
		this.setOpaque(false);
		this.add(getJScrollPane());
		this.add(getButtonJoinGame());
		this.add(labelTextPlayerName);
		this.add(getTextFieldEnterName());
		this.add(getButtonBack());
		this.add(labelWaiting);
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(30, 30, 136, 331));
			jScrollPane.setViewportView(getJList());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes buttonJoinGame
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getButtonJoinGame() {
		if (buttonJoinGame == null) {
			buttonJoinGame = new JButton();
			buttonJoinGame.setBounds(new Rectangle(210, 30, 166, 61));
			buttonJoinGame.setText("Join Game");
			buttonJoinGame
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							GuiMainFrame.getClient().setPlayerName(
									textFieldEnterName.getText());
							int gameID = 0;
							try {
//								for (Game currentGame : listOfGames) {
//									if (currentGame.getTitle().equals(
//											(String) jList.getSelectedValue())) {
//										gameID = currentGame.getGameID();
//										break;
//									}
//								}
								GuiMainFrame.getClient().joinGame(((Game)jList.getSelectedValue()).getGameID());
							} catch (NumberFormatException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							} catch (JSONException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							} catch (ServerCommunicationException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							} catch (GameEndedException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							buttonJoinGame.setEnabled(false);
							checkIfGameIsStarted.start();
						}
					});
		}
		return buttonJoinGame;
	}

	/**
	 * This method initializes jList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getJList() {
		if (jList == null) {
			// listet die Games
//			String[] gameNames;
			Game[] games;
			try {
//				listOfGames = GuiMainFrame.getClient().getGames();
//				gameNames = new String[listOfGames.size()];
//				for (int x = 0; x < listOfGames.size(); x++) {
//					gameNames[x] = listOfGames.get(x).getTitle();
//				}
				jList = new JList(GuiMainFrame.getClient().getGames().toArray());
			} catch (JSONException e) {
				String[] noGames = { "Error", "Error" };
				jList = new JList(noGames);
				e.printStackTrace();
			}
		}
		return jList;
	}

	/**
	 * This method initializes textFieldEnterName
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTextFieldEnterName() {
		if (textFieldEnterName == null) {
			textFieldEnterName = new JTextField();
			textFieldEnterName.setBounds(new Rectangle(210, 165, 166, 46));
			textFieldEnterName.setText("enter new name");
		}
		return textFieldEnterName;
	}

	/**
	 * This method initializes buttonBack
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getButtonBack() {
		if (buttonBack == null) {
			buttonBack = new JButton();
			buttonBack.setBounds(new Rectangle(210, 300, 166, 61));
			buttonBack.setText("Back");
			buttonBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					((GuiMainFrame) SwingUtilities
							.getRoot(ListAllGamesPanel.this))
							.switchActionPanel(new MainMenuPanel());
				}
			});
		}
		return buttonBack;
	}

	private Thread checkIfGameIsStarted = new Thread(new Runnable() {
		boolean isGameStarted = false; // @jve:decl-index=0:

		@Override
		public void run() {
			while (!isGameStarted) {
				try {
					GuiMainFrame.getClient().getDrawnWords();
					// kommt keine Exception ist das Spiel gestartet
					// und es kann das Spielfeld aufgerufen werden.
					isGameStarted = true;
					Thread.sleep(100);
					// spielbrett aufrufen
					((GuiMainFrame)SwingUtilities.getRoot(ListAllGamesPanel.this)).switchActionPanel(new GameBoardPanel());
				} catch (JSONException e) {
				} catch (ServerCommunicationException e) {
				} catch (GameNotStartetException e) {
					labelWaiting.setVisible(true);
				} catch (InterruptedException e) {
				} catch (GameEndedException e) {

				}
			}
		}
	});

}
